using GameConfig.item;
using GameLogic.GameLogic;
using UnityEngine;

namespace GameLogic
{
    public class PlayerSlotInventoryPanel : MonoBehaviour
    {
        public SlotUI[] slotUIs=new SlotUI[10];
        public int currentSelectIndex = -1;
        public ItemTb currentSelectItem;

        public static PlayerSlotInventoryPanel instance;

        private void Awake()
        {
            if(instance==null)
                instance = this;
        }

        public void InitPlayerSlotData()
        {
            foreach (var item in UserInfo.Instance.GetUserSlotInfo())
            {
                
                if (ConfigSystem.Instance.Tables.TbItem.DataMap.ContainsKey(item.Value.itemID))
                {
                    ItemTb itemTb = ConfigSystem.Instance.Tables.TbItem.Get(item.Value.itemID);
                    slotUIs[item.Value.itemPos].UpdateSlot(itemTb, item.Value.itemAmount);
                    slotUIs[item.Value.itemPos].slotIndex = item.Value.itemPos;
                    slotUIs[item.Value.itemPos].slotType=SlotType.SlotUI;
                }
                else
                {
                    slotUIs[item.Key].UpdateSlot(null, item.Value.itemAmount);
                    slotUIs[item.Value.itemPos].slotType = SlotType.SlotUI;
                    slotUIs[item.Key].slotIndex = item.Key;
                }
            }
        }

        public void SelectSlotUI(int index,bool isSelect=false)
        {
            if (isSelect)
            {
                if (currentSelectIndex != -1)
                {
                    slotUIs[currentSelectIndex].IsSelect(false);
                }
                currentSelectIndex = index;
                slotUIs[index].IsSelect(true);
                if (slotUIs[index].itemDetails != null)
                {
                    currentSelectItem = slotUIs[index].itemDetails;
                }
                return;
            }
            if (index != currentSelectIndex)
            {
                if (currentSelectIndex != -1)
                {
                    slotUIs[currentSelectIndex].IsSelect(false);
                }
                currentSelectIndex = index;
                slotUIs[index].IsSelect(true);
                if (slotUIs[index].itemDetails != null)
                {
                    currentSelectItem = slotUIs[index].itemDetails;
                }
            }
            else
            {
                slotUIs[currentSelectIndex].IsSelect(false);
                currentSelectIndex = -1;
            }
        }
    }
}
